package com.ks.core.base;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by WangLiCheng:nomad21st@163.com:KingDee Co. on 2020/12/20.
 */
public class ParamConvert {
    public Long paramStringToLong(String value, Long def){
        Long longValue = null;

        try{
            longValue = Long.parseLong(paramStringToString(value, ""));
        } catch (Exception ex){
            longValue = def;
        }

        return longValue;
    }
    public Boolean paramStringToBoolean(String value, Boolean def){
        Boolean bValue = false;
        try{
            bValue = Boolean.valueOf(paramStringToString(value,""));
        } catch (Exception ex){
            bValue = def;
        }
        return bValue;
    }
    public Integer paramStringToInteger(String value, Integer def){
        Integer intValue = null;
        try{
            intValue = Integer.parseInt(paramStringToString(value, ""));
        } catch (Exception ex){
            intValue = def;
        }
        return intValue;
    }
    public Date paramStringToDate(String value, Date def)
    {
        Date date =null;
        try
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            date = sdf.parse(paramStringToString(value, ""));
        }
        catch (Exception e)
        {
            date=def;
        }
        return date;
    }
    public Date paramStringToLongDate(String value,Date def)
    {
        Date date =null;
        try
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            date = sdf.parse(paramStringToString(value, ""));
        }
        catch (Exception e)
        {
            date=def;
        }
        return date;
    }
    public String paramStringToString(String value, String def){
        String strValue = null;

        try{
            if (value.length() == 0){
                strValue = def;
            } else {
                String[] values = value.split("/");
                strValue = values[values.length-1];
            }
        } catch (Exception ex){
            strValue = def;
        }

        return strValue;
    }

    public Long paramStringToLong(String value){
        return paramStringToLong(value,null);
    }
    public Boolean paramStringToBoolean(String value){
        return paramStringToBoolean(value,null);
    }
    public Integer paramStringToInteger(String value){
        return paramStringToInteger(value,null);
    }
    public Date paramStringToDate(String value)
    {
        return paramStringToDate(value,null);
    }
    public Date paramStringToLongDate(String value)
    {
        return paramStringToLongDate(value,null);
    }
    public String paramStringToString(String value){
        return paramStringToString(value,"");
    }
}
